package com.xm.xmapp.customview;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TextView;

import com.xm.xmapp.R;
import com.xm.xmapp.myinterface.MyDialogButton;

public class LongRentDateTimePickDialog extends Dialog implements
		OnDateChangedListener {

	private DatePicker datePicker;
	private TextView left, right, title;

	Activity activity;
	private String initDateTime;
	private String dateTime;
	private MyDialogButton in;

	public void setInitDateTime(String initDateTime) {
		this.initDateTime = initDateTime;
	}

	public LongRentDateTimePickDialog(Context context, int theme) {
		super(context, theme);
	}

	public LongRentDateTimePickDialog(Activity activity, MyDialogButton in) {
		this(activity, R.style.share_Dialog);
		this.activity = activity;
		this.in = in;

	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {

		return super.onTouchEvent(event);

	}

	public void setStrTitle(String title_str) {
		title.setText(title_str);

	}

	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_longrent_datetimepicker);
		datePicker = (DatePicker) findViewById(R.id.datepicker);
		left = (TextView) findViewById(R.id.left);
		right = (TextView) findViewById(R.id.right);
		title = (TextView) findViewById(R.id.title);

		datePicker.setVisibility(View.VISIBLE);
		init(datePicker);

		Window window = getWindow();
		WindowManager.LayoutParams wl = window.getAttributes();
		wl.alpha = 1.0f;

		wl.gravity = Gravity.BOTTOM;
		WindowManager wm = activity.getWindowManager();
		wl.width = wm.getDefaultDisplay().getWidth();
		window.setAttributes(wl);
		setCancelable(true);
		setCanceledOnTouchOutside(true);

		left.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub

				dismiss();
			}
		});
		right.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {

				if (in != null)
					in.setTitle(0, dateTime);

				dismiss();
			}
		});

	}

	public void init(DatePicker datePicker) {
		Calendar calendar = Calendar.getInstance();

		if (!(null == initDateTime || "".equals(initDateTime))) {
			dateTime = initDateTime;
			try {
				Date date1 = sdf.parse(initDateTime);
				calendar.setTime(date1);
			} catch (Exception e) {
			}

		} else {

			calendar.add(Calendar.DAY_OF_MONTH, +1);//+1今天的时间加一天
			dateTime = calendar.get(Calendar.YEAR) + "-"
					+ (calendar.get(Calendar.MONTH) + 1) + "-"
					+ calendar.get(Calendar.DAY_OF_MONTH);

			initDateTime = calendar.get(Calendar.YEAR) + "-"
					+ calendar.get(Calendar.MONTH) + "-"
					+ calendar.get(Calendar.DAY_OF_MONTH);

		}

		datePicker.init(calendar.get(Calendar.YEAR),
				calendar.get(Calendar.MONTH),
				calendar.get(Calendar.DAY_OF_MONTH), this);

	}

	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

	@Override
	public void onDateChanged(DatePicker view, int year, int monthOfYear,
			int dayOfMonth) {
		// TODO Auto-generated method stub
		// 获得日历实例

		if (isDateAfter(view)) {

			view.init(maxCalendar.get(Calendar.YEAR),
					maxCalendar.get(Calendar.MONTH),
					maxCalendar.get(Calendar.DAY_OF_MONTH), this);
			dateTime = sdf.format(maxCalendar.getTime());

		} else if (isDatebefore(view)) {

			Calendar mCalendar = Calendar.getInstance();
			view.init(mCalendar.get(Calendar.YEAR),
					mCalendar.get(Calendar.MONTH),
					mCalendar.get(Calendar.DAY_OF_MONTH), this);
			dateTime = sdf.format(mCalendar.getTime());

		} else {

			Calendar calendar = Calendar.getInstance();

			calendar.set(datePicker.getYear(), datePicker.getMonth(),
					datePicker.getDayOfMonth(), 0, 0);

			dateTime = sdf.format(calendar.getTime());
			this.setTitle(dateTime);
		}
	}

	Calendar maxCalendar = Calendar.getInstance();

	public void setMaxCalendar(int day) {

		Date date = new Date();// 当前时间
		maxCalendar.setTime(date);// 设置当前日期
		maxCalendar.add(Calendar.DAY_OF_MONTH, day);
	}

	private boolean isDateAfter(DatePicker tempView) {

		Calendar tempCalendar = Calendar.getInstance();

		tempCalendar.set(tempView.getYear(), tempView.getMonth(),
				tempView.getDayOfMonth(), 0, 0, 0);
		if (tempCalendar.after(maxCalendar))
			return true;
		else
			return false;
	}

	private boolean isDatebefore(DatePicker tempView) {
		Calendar minCalendar = Calendar.getInstance();
		Calendar tempCalendar = Calendar.getInstance();

		tempCalendar.set(tempView.getYear(), tempView.getMonth(),
				tempView.getDayOfMonth(), 0, 0, 0);
		if (tempCalendar.before(minCalendar))
			return true;
		else
			return false;
	}
}
